//
//  HMWelecomeViewController.m
//  OC新浪微博
//
//  Created by wyman on 16/4/13.
//  Copyright © 2016年 wyman. All rights reserved.
//

#import "HMWelecomeViewController.h"
#import <Masonry.h>
#import "AppDelegate.h"
#import "HMMainViewController.h"
#import "HMUserAccountModel.h"
#import <AFNetworking/AFNetworking.h>
#import <UIImageView+WebCache.h>
@interface HMWelecomeViewController ()

@end

@implementation HMWelecomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加控件
    [self prepareUI];
    NSURL *iconImage = [NSURL URLWithString:[HMUserAccountModel sharedInstance].userAccount.avatar_large];
    
    
    [self.iconView sd_setImageWithURL:iconImage placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
//    NSURL *iconImage = [NSURL URLWithString:[HMUserAccountModel sharedInstance].userAccount.avatar_large];
//    
//    
//    [self.iconView sd_setImageWithURL:iconImage placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
    [[HMUserAccountModel sharedInstance] loadUserInfoBlock:^(NSError *error) {
        if (error !=nil){
            HMLog(@"加载用户信息出错");
            return;
        }
        //加载图片
        NSURL *iconImage = [NSURL URLWithString:[HMUserAccountModel sharedInstance].userAccount.avatar_large];
        
        
        [self.iconView sd_setImageWithURL:iconImage placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
        
    }];
    //加载图片
    
    
}

//添加动画

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self startAV];
    

}
-(void)startAV
{
   [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
    make.bottom.equalTo(self.view).offset(-([UIScreen mainScreen].bounds.size.height -160));
    
   }];
    
    //添加动画
    [UIView animateWithDuration:2 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:5 options:0 animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        HMLog(@"动画完成了,这里切换动画")
        UIViewController *controller = [[HMMainViewController alloc]init];
        
        
        [(AppDelegate *)([UIApplication sharedApplication].delegate) switchViewController:controller];
        
    }];



}
-(void)prepareUI{
//添加三个控件
    self.bgImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ad_background"]];
    
    [self.view addSubview:self.bgImageView];
    
   [self.view addSubview:self.iconView];
    
    self.welLaebl = [[UILabel alloc]init];
    self.welLaebl.text = @"欢迎归来";
    [self.view addSubview:self.welLaebl];


    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-160);
    }];
    
    [self.welLaebl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconView);
        make.top.equalTo(self.iconView.mas_bottom).offset(16);
        
    }];
}

-(UIImageView *)iconView
{
    if (_iconView == nil) {
        _iconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"avatar_default_big"]];

        //切圆
        _iconView.layer.cornerRadius = 42.5;
        [_iconView.layer masksToBounds];
    }



    return _iconView;
}
@end
